package org.balthie.cal.core;

import java.util.Deque;

import org.balthie.cal.common.spec.ISpecification;

public abstract class AbsCommand implements ICommand {

	// 指令集暂存区规格
	protected ISpecification<Deque<ICommand>> workAreaSpecs;

	// 当前新增指令规格
	protected ISpecification<ICommand> currentCmdSpecs;

	// 算式合法性检查
	protected ISpecification<Deque<ICommand>> legalSpecs;
	
	protected String literallyContent;

	// 命令实例对象 所属的计算器实例对象
	AbsCalculator cal;

	protected AbsCommand(String literallyContent) {
		super();
		this.literallyContent = literallyContent;
	}

	@Override
	public String getLiterally() {
		return literallyContent;
	}

	@Override
	public boolean isCompleted(Deque<ICommand> deque) {
		if (this.workAreaSpecs == null) {
			return Boolean.FALSE;
		}
		// 同时通过 指令集暂存区规格检查 + 当前新增指令规格检查
		return this.workAreaSpecs.isSatisfiedBy(deque)
				&& (this.currentCmdSpecs != null ? this.currentCmdSpecs.isSatisfiedBy(this) : Boolean.TRUE);
	}

	@Override
	public void isLegal(Deque<ICommand> workArea, Deque<ICommand> undoArea) {
		// 输入新指令，执行算式合法性规格检查
		legalSpecs.check(workArea);
	}

	@Override
	public void execute(Deque<ICommand> deque, Deque<ICommand> undoArea) {
		// 所有指令默认不执行运算
	}

	protected void println(Object... args) {
		this.cal.println(args);
	}

	protected void print(Object... args) {
		this.cal.print(args);
	}
}
